import { createRouter, createWebHistory } from 'vue-router'
import IndexView from "@/views/admin/IndexView.vue";
import HomeView from "@/views/admin/HomeView.vue";
import UserHomeView from "@/views/user/HomeView.vue";
import AdminView from "@/views/admin/AdminView.vue";
import CategoryView from "@/views/admin/CategoryView.vue";
import GoodsView from "@/views/admin/GoodsView.vue";
import UserGoodsView from "@/views/user/GoodsView.vue";
import OrderView from "@/views/admin/OrderView.vue";
import UserOrderListView from "@/views/user/center/OrderListView.vue";
import UserView from "@/views/admin/UserView.vue";
import LoginView from "@/views/admin/LoginView.vue";
import UserLoginView from "@/views/user/LoginView.vue";
import UserIndexView from "@/views/user/IndexView.vue"
import {useTokenStore} from "@/stores/token.js";
import SearchView from "@/views/user/SearchView.vue";
import RegView from "@/views/user/RegView.vue";
import CartView from "@/views/user/CartView.vue";
import CreateOrderView from "@/views/user/CreateOrderView.vue";
import CenterView from "@/views/user/CenterView.vue";
import AddrListView from "@/views/user/center/AddrListView.vue";
import InfoView from "@/views/user/center/InfoView.vue";
import PasswordView from "@/views/user/center/PasswordView.vue";
import RechargeView from "@/views/user/center/RechargeView.vue";
import CollectListView from "@/views/user/center/CollectListView.vue";
import PayView from "@/views/user/payView.vue";



const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/admin/login',
      name: 'admin_login',
      component:LoginView,
    },
    {
      path: '/admin/home',
      name: 'admin_home',
      component: HomeView,
      redirect:'/admin/index',
      children:[
        {
          path:"/admin/index",
          name:"admin_index",
          component:IndexView
        },
        {
          path:"/admin/admin",
          name:"admin_admin",
          component:AdminView
        },
        {
          path:"/admin/category",
          name:"admin_category",
          component:CategoryView
        },
        {
          path:"/admin/goods",
          name:"admin_goods",
          component:GoodsView
        },
        {
          path:"/admin/order",
          name:"admin_order",
          component:OrderView
        },
        {
          path:"/admin/user",
          name:"admin_user",
          component:UserView
        },
      ]

    },
    {
      path: '/user/login',
      name: 'user_login',
      component: UserLoginView,
    },
    {
      path: '/',
      name: 'user_home',
      component: UserHomeView,
      redirect:'user/index',
      children: [
        {
          path:"/user/index",
          name:"user_index",
          component:UserIndexView
        },
        {
          path:"/user/reg",
          name:"user_reg",
          component:RegView
        },
        {
          path:"/user/search/:categoryId?",
          props:true,
          name:"user_search",
          component:SearchView
        },
        {
          path:"/user/goods",
          name:"user_goods",
          component:UserGoodsView
        },
        {
          path:"/user/cart",
          name:"user_cart",
          component:CartView
        },
        {
          path:"/user/pay",
          name:"user_pay",
          component:PayView
        },
        {
          path:"/user/createOrder",
          name:"user_createOrder",
          component:CreateOrderView
        },
        {
          path:"/user/center",
          name:"user_center",
          component:CenterView,
          redirect:'/user/info',
          children:[
            {
              path:"/user/addr",
              name:"user_addr",
              component:AddrListView
            },
            {
              path:"/user/orderList",
              name:"user_orderList",
              component:UserOrderListView
            },
            {
              path:"/user/collect",
              name:"user_collect",
              component:CollectListView
            },
            {
              path:"/user/password",
              name:"user_password",
              component:PasswordView
            },
            {
              path:"/user/info",
              name:"user_info",
              component:InfoView
            },
            {
              path:"/user/recharge",
              name:"user_recharge",
              component:RechargeView
            },
          ]
        },
      ]
    }
    // {
    //   path: '/about',
    //   name: 'about',
    //   // route level code-splitting
    //   // this generates a separate chunk (About.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () => import('../views/AboutView.vue')
    // }
  ]
})
router.beforeEach((to, from, next) => {
  // 如果目标是登录页面，直接放行
  //TODO 将来对前台首页，搜索页，用户登录注册页 放行
  if (to.path === '/admin/login'|| to.path === '/user/login'||
      to.path === "/user/reg" ||  to.path.startsWith("/user/search")
      || to.path === "/user/index"
      || to.path === "/user/goods"
      || to.path ==="/"
  ) {
    next();
  } else {
    const tokenStore=useTokenStore();
    // 检查 sessionStorage 中是否有 token
    const token = tokenStore.tokenStr;
    if (!token) {
     let path=router.currentRoute.value.path
      if(path.startsWith("/admin")){
        // 没有 token，重定向到登录页面
        next('/admin/login');
      }else {
        next('/user/login');
      }

    } else {

      next();
    }
  }
});
export default router
